<?php

/**
 * WZ form.
 *
 * @package    ProdAssistant
 * @subpackage form
 * @author     Paweł Świtalla
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class WZForm extends BaseWZForm
{
  public function configure()
  {
    $this->useFields(array('project_id', 'annotation', 'items_list'));
    
    $this->validatorSchema->setOption('allow_extra_fields', true);
    
    $query = Doctrine_Query::create()
            ->select('i.*')
            ->from('StoreItem i')
            ->where('i.quantity != ?', 0)
            ->orderBy('i.name ASC');
    $item = $query->fetchOne();
    
    if(!($item instanceof StoreItem)){
      $this->setWidget('no_items', new sfWidgetFormInputHidden());
      $firstId = 0;
    }
    else{
      $firstId = $item->getId();
    }
    
    $this->getWidget('items_list')
            ->setOption('multiple', '')
            ->setOption('query', $query)
            ->setOption('method', 'getNameForDemand')
            ->setAttribute('name', 'wz[items_list][]')
            ->setAttribute('onchange', "var value = $(this).prop('value'); App.Form.updateAttribute('#items_list_values_1', 'name', 'wz[items_list_values]['+value+']')");
    
    $this->setWidget('items_list_values', new sfWidgetFormInput());
    
    $this->getWidget('items_list_values')
            ->setAttribute('id', 'items_list_values_1')
            ->setAttribute('name', 'wz[items_list_values]['.$firstId.']');
    
    if($this->getWidget('project_id') instanceof sfWidgetForm)
    {
      $core = PACore::getInstance();
      $this->getWidget('project_id')->setLabel('Projekt <br /><a onclick="App.loadWindow(\''.$core->generateAppUrl('Core', 'homeGetComponent', true, array('componentModule' => 'ProductionSettings', 'name' => 'manageProjects', 'handler' => 'refreshLastForm')).'\')" class="active"><img src="/images/icons/add.png" class="middle" height="18" width="18"></a>');
    }
  }
}
